package cn.sumpu.app.guoguojie.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.constant.MessageType;
import cn.sumpu.app.guoguojie.dao.MessageDao;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Message;
import cn.sumpu.app.guoguojie.domain.MessageAmount;
import cn.sumpu.app.guoguojie.domain.Ext.MessageExt;
import cn.sumpu.app.guoguojie.util.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

public class MessageBizImpl implements MessageBiz {

    @Resource
    private MemberBiz     memberBiz;
    @Resource
    private IdentifierBiz identifierBiz;

    private MessageDao    messageDao;

    public void setMessageDao(MessageDao messageDao) {
        this.messageDao = messageDao;
    }

    /**
     * 更新未读消息(系统)
     * @param memberId
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#readMessageSystem(java.lang.String)
     */
    @Override
    public void readMessageSystem(String memberId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveMember", memberId);
        map.put("type", MessageType.SYSTEM.getValue());
        messageDao.updateMessage(map);
    }

    /**
     *  更新未读消息（站内信）
     * @param letterBox
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#readMessageInner(java.lang.String)
     */
    @Override
    public void readMessageInner(String memberId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveMember", memberId);
        map.put("type", MessageType.INNER.getValue());
        messageDao.updateMessage(map);
    }

    /**
     * 查询系统消息
     * @param memberId
     * @param page
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#getMessageSystem(java.lang.String, java.lang.Integer)
     */
    @Override
    public PageModel getMessageSystem(String memberId, Integer page) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveMember", memberId);
        map.put("type", MessageType.SYSTEM.getValue());
        Integer startId = (page - 1) * Common.PAGE_SIZE_MESSAGE;
        map.put("startId", startId);
        map.put("pageSize", Common.PAGE_SIZE_MESSAGE);
        List<MessageExt> msgList = messageDao.getMessageList(map);
        int count = messageDao.getMessageListCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        pm.setList(msgList);
        pm.setPageSize(Common.PAGE_SIZE_MESSAGE);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + Common.PAGE_SIZE_MESSAGE - 1) / Common.PAGE_SIZE_MESSAGE);
        return pm;
    }

    /**
     * 删除系统消息
     * @param id
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#deleteMessageById(cn.sumpu.app.guoguojie.domain.Member, java.lang.String)
     */
    @Override
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    public void deleteMessageById(String id) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", id);
        //删除父消息 
        messageDao.deleteMessageByMap(map);
        Map<String, Object> mapR = new HashMap<String, Object>();
        mapR.put("replyMessageId", id);
        //删除子消息
        messageDao.deleteMessageByMap(mapR);
    }

    /**
     * 查询站内信消息
     * @param memberId
     * @param page
     * @return
     * @throws AppBusinessException
     */
    @Override
    public PageModel getMessageInner(String memberId, Integer page) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sendMember", memberId);
        map.put("receiveMember", memberId);
        map.put("type", MessageType.INNER.getValue());
        Integer startId = (page - 1) * Common.PAGE_SIZE_MESSAGE;
        map.put("startId", startId);
        map.put("pageSize", Common.PAGE_SIZE_MESSAGE);
        List<MessageExt> msgList = messageDao.getMessageList(map);
        int count = messageDao.getMessageListCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        pm.setList(msgList);
        pm.setPageSize(Common.PAGE_SIZE_MESSAGE);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + Common.PAGE_SIZE_MESSAGE - 1) / Common.PAGE_SIZE_MESSAGE);
        return pm;

    }

    /**
     * 消息详情
     * @param msgId
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#getMessage(java.lang.String)
     */
    @Override
    public Message getMessage(String msgId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("identifier", msgId);
        Message message = messageDao.getMessage(map);
        return message;
    }

    /**
     * 添加消息
     * @param message
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#insertMessage(cn.sumpu.app.guoguojie.domain.Message)
     */
    @Override
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    public void insertMessage(Message message) throws AppBusinessException {
        message.setIdentifier(identifierBiz.generateId("message"));
        messageDao.insertMessage(message);
        // 更新未读消息数量
        updateMessageAmount(message.getReceiveMember(), 1, MessageType.INNER.getValue());
    }

    /**
     * 发站内信
     * @param allPost
     * @param nickname
     * @param content
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#addClientFeedback(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void addClientFeedback(String allPost, String nickname, String content) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        if ("整站发布".equals(allPost)) {
            map.put("allPost", "整站发布");
            map.put("content", content);
        } else {
            List<Message> messages = new ArrayList<Message>();
            Message m = null;
            Member member = null;
            String[] nicknameArr = nickname.split(",");
            for (String n : nicknameArr) {
                member = memberBiz.getMemberByNickname(n.trim());
                if (member != null) {
                    m = new Message(identifierBiz.generateId("message"), content, member.getIdentifier());
                    messages.add(m);
                }
            }
            map.put("list", messages);
        }
        messageDao.addClientFeedback(map);
    }

    /**
     * 更新未读消息数量+1
     * @param family
     * @param unreads
     * @param type
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#updateMessageAmount(java.lang.String, int, java.lang.String)
     */
    @Override
    public void updateMessageAmount(String memberId, int unreads, String type) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("member", memberId);
        map.put("type", type); 
        MessageAmount messageAmount = getMessageAmountById(memberId, type);
        if (messageAmount != null) {
            map.put("unreads", messageAmount.getUnreads() + 1);
            messageDao.updateMessageAmount(map);
        } else {
            MessageAmount newMsga = new MessageAmount();
            newMsga.setMember(memberId);
            newMsga.setUnreads(1);
            newMsga.setType(type);
            insertMessageAmount(newMsga);
        }
    }

    /**
     * 未读消息
     * @param memberId
     * @param type
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#getMessageAmountById(java.lang.String, java.lang.String)
     */
    @Override
    public MessageAmount getMessageAmountById(String memberId, String type) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("member", memberId);
        map.put("type", type);
        return messageDao.getMessageAmountByMap(map);
    }

    /**
     * 新建未读消息
     * @param messageAmount
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#insertMessageAmount(cn.sumpu.app.guoguojie.domain.MessageAmount)
     */
    @Override
    public void insertMessageAmount(MessageAmount messageAmount) throws AppBusinessException {
        messageDao.insertMessageAmount(messageAmount);
    }
    /**
     * 未读站内信数量
     * @param memberId
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#messageInnerNoReadCount(java.lang.String)
     */
    @Override
    public int messageInnerNoReadCount(String memberId) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveMember", memberId);
        map.put("type", MessageType.INNER.getValue());
        return messageDao.getMessageNoReadCount(map);
    }
    /**
     * 通知未读消息数量
     * @param receiveMember
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.MessageBiz#messageSysNoReadCount(java.lang.String)
     */
    @Override
    public int messageSysNoReadCount(String receiveMember) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveMember", receiveMember);
        map.put("type", MessageType.SYSTEM.getValue());
        return messageDao.getMessageNoReadCount(map);
    }
    
    /**
     * 未读消息+1
     * @param memberId
     * @param type
     * @throws AppBusinessException
     */
    public void messageAmount(String memberId,String type)throws AppBusinessException{
            updateMessageAmount(memberId, 1, type);
    }
    
    
    
    
    
    
    
    
    
    
    
    

   /* @Override
    public PageModel getMessageFriend(String receiveFamily, Integer page) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveFamily", receiveFamily);
        map.put("type", MessageType.FRIEND.getValue());
        Integer startId = (page - 1) * Common.PAGE_SIZE_MESSAGE;
        map.put("startId", startId);
        map.put("pageSize", Common.PAGE_SIZE_MESSAGE);
        List<MessageExt> msgList = messageDao.getMessageFriend(map);
        int count = messageDao.getMessageFriendCount(map);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        pm.setList(msgList);
        pm.setPageSize(Common.PAGE_SIZE_MESSAGE);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + Common.PAGE_SIZE_MESSAGE - 1) / Common.PAGE_SIZE_MESSAGE);
        return pm;
    }

    @Override
    public int getMessageNoReadCount(String family) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("receiveFamily", family);
        return messageDao.getMessageNoReadCount(map);
    }

    @Override
    @Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
    public Message insertMessage(Member member, Message message) throws AppBusinessException {
        if (message == null)
        	return null;
        // 他家信箱
        message.setIdentifier(identifierBiz.generateId("message"));
        message.setIsRead(false);
        messageDao.insertMessage(message);

        // 自己家信箱
        message.setIdentifier(identifierBiz.generateId("message"));
        message.setIsRead(true);
        message.setLetterBox(message.getSendOwner());
        message.setLetterBoxType(message.getSendOwnerType());

        messageDao.insertMessage(message);

        return message;
    }

    @Override
    public int messageAllNoReadCount(String receiveFamily) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("letterBox", receiveFamily);
        map.put("letterBoxType", OwnerType.FAMILY.getValue());
        return messageDao.getMessageNoReadCount(map);
    }

    @Override
    public int messageFriendNoReadCount(String receiveFamily) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("letterBox", receiveFamily);
        map.put("letterBoxType", OwnerType.FAMILY.getValue());
        map.put("type", MessageType.FRIEND.getValue());
        return messageDao.getMessageNoReadCount(map);
    }

    

    @Override
    public void readMessageAll(String receiveFamily) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("letterBox", receiveFamily);
        map.put("letterBoxType", OwnerType.FAMILY.getValue());
        messageDao.updateMessage(map);
    }

    @Override
    public void readMessageFriend(String receiveFamily) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("letterBox", receiveFamily);
        map.put("letterBoxType", OwnerType.FAMILY.getValue());
        map.put("type", MessageType.FRIEND.getValue());
        messageDao.updateMessage(map);
    }

    @Override
    public void insertSystemMessage(String family, String content) throws AppBusinessException {
        	Message message = new Message();
        	message.setIdentifier(identifierBiz.generateId("message"));
        	message.setType(MessageType.SYSTEM.getValue());
        	message.setContent(content);
        	message.setLetterBox(family);
        	message.setLetterBoxType(OwnerType.FAMILY.getValue());
        	message.setReceiveOwner(family);
        	message.setReceiveOwnerType(OwnerType.FAMILY.getValue());
        	messageDao.insertMessage(message);
    }

    @Override
    public void deleteMessage(String id, String letterBox, String letterBoxType)
    		throws AppBusinessException {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("identifier", id);
    	map.put("letterBox", letterBox);
    	map.put("letterBoxType", letterBoxType);
    	messageDao.deleteMessage(map);
    }

    @Override
    public List<MessageAmount> getMessageAmount(String family) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("family", family);
        return messageDao.getMessageAmount(map);
    }

    @Override
    public void messageAmount(String family, String type) throws AppBusinessException {
    }*/

}
